home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / editors / eedraw / src / eep / eeload.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-23  |  7.4 KB  |  244 lines

  1. /*****************************************************************************
  2. *   Module to load EEDraw files.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber            IBM PC Ver 1.0,    Oct. 1989    *
  5. *****************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #ifdef __MSDOS__
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <dos.h>
  14. #include <alloc.h>
  15. #endif /* __MSDOS__ */
  16.  
  17. #include "program.h"
  18. #include "eelibs.h"
  19. #include "eeredraw.h"
  20. #include "eeload.h"
  21. #include "eelayer.h"
  22. #include "virtrstr.h"
  23.  
  24. #define EEDRAW_FILE_STAMP "EEDraw"
  25. #define NULL_STRING "_NONAME_"
  26.  
  27. /*****************************************************************************
  28. * Routine to load an EEDraw file.                         *
  29. * Save in LoadedFileName the name of the file loaded to reset it to an       *
  30.  * empty string if none loaded.                             *
  31. *****************************************************************************/
  32. void LoadEEFile(FILE *f)
  33. {
  34.     char Line[LINE_LEN], Name1[LINE_LEN_SHORT], Name2[LINE_LEN_SHORT],
  35.              Char1[LINE_LEN_SHORT], Char2[LINE_LEN_SHORT];
  36.     int i, LineCount = 1;
  37.     BooleanType Failed = FALSE;
  38.     DrawGenericStruct *Phead, *Pnext;
  39.     DrawLibItemStruct *LibItemStruct;
  40.     DrawConnectionStruct *ConnectionStruct;
  41.     DrawPolylineStruct *PolylineStruct;
  42.     DrawTextStruct *TextStruct;
  43.  
  44.     if (fgets(Line, LINE_LEN - 1, f) == NULL ||
  45.     strncmp(Line, EEDRAW_FILE_STAMP, sizeof(EEDRAW_FILE_STAMP) - 1) != 0) {
  46.     fclose(f);
  47.     FatalError("Given file is NOT EEDraw file");
  48.     }
  49.  
  50.     if (fgets(Line, LINE_LEN - 1, f) == NULL ||
  51.     strncmp(Line, "LIBS:", 5) != 0) {
  52.     fclose(f);
  53.     FatalError("Given file is NOT EEDraw file");
  54.     }
  55.     else
  56.     LoadLibraries(&Line[5]);
  57.  
  58. SeedLayers();            /* Set the default Layers up + Layer Array */
  59. LoadLayers(f);
  60.     while (!feof(f) &&
  61.        fgets(Line, LINE_LEN - 1, f) != NULL) {
  62.     LineCount++;
  63.  
  64.     switch(Line[0]) {
  65.         case 'L':                      /* Its a library item. */
  66.         LibItemStruct = (DrawLibItemStruct *)
  67.                         MyMalloc(sizeof(DrawLibItemStruct));
  68.         LibItemStruct -> StructType = DRAW_LIB_ITEM_STRUCT_TYPE;
  69.         if (sscanf(&Line[1], "%s %s %s %s %d %d %d %d",
  70.                Name1, Name2,
  71.                Char1, Char2,
  72.                &LibItemStruct -> ChipNameX,
  73.                &LibItemStruct -> ChipNameY,
  74.                &LibItemStruct -> PartNameX,
  75.                &LibItemStruct -> PartNameY) != 8 ||
  76.             (Char1[0] != 'V' && Char1[0] != 'H') ||
  77.             (Char2[0] != 'V' && Char2[0] != 'H')) {
  78.             sprintf(Line,
  79.             "EEDraw file lib item struct error at line %d, aborted",
  80.                                 LineCount);
  81.             Failed = TRUE;
  82.             break;
  83.         }
  84.         LibItemStruct -> ChipNameOrient =
  85.             Char1[0] == 'V' ? TEXT_ORIENT_VERT : TEXT_ORIENT_HORIZ;
  86.         LibItemStruct -> PartNameOrient =
  87.             Char2[0] == 'V' ? TEXT_ORIENT_VERT : TEXT_ORIENT_HORIZ;
  88.  
  89.         if (strcmp(Name1, NULL_STRING) != 0) {
  90.             for (i = 0; i < strlen(Name1); i++)
  91.             if (Name1[i] == '~') Name1[i] = ' ';
  92.             LibItemStruct -> ChipName = strdup(Name1);
  93.         }
  94.         else {
  95.             LibItemStruct -> ChipName = NULL;
  96.             LibItemStruct -> ChipNameOrient = TEXT_ORIENT_NON;
  97.         }
  98.  
  99.         if (strcmp(Name2, NULL_STRING) != 0) {
  100.             for (i = 0; i < strlen(Name2); i++)
  101.             if (Name2[i] == '~') Name2[i] = ' ';
  102.             LibItemStruct -> PartName = strdup(Name2);
  103.         }
  104.         else {
  105.             LibItemStruct -> PartName = NULL;
  106.             LibItemStruct -> PartNameOrient = TEXT_ORIENT_NON;
  107.         }
  108.  
  109.         LineCount++;
  110.         if (!Failed &&
  111.             (fgets(Line, LINE_LEN - 1, f) == NULL ||
  112.              sscanf(Line, "%d %d %d %d %d %d %d",
  113.                 &LibItemStruct -> Multi,
  114.                 &LibItemStruct -> PosX,
  115.                 &LibItemStruct -> PosY,
  116.                 &LibItemStruct -> BBoxMinX,
  117.                 &LibItemStruct -> BBoxMinY,
  118.                 &LibItemStruct -> BBoxMaxX,
  119.                 &LibItemStruct -> BBoxMaxY) != 7)) {
  120.             Failed = TRUE;
  121.             break;
  122.         }
  123.  
  124.         LineCount++;
  125.         if (!Failed &&
  126.             (fgets(Line, LINE_LEN - 1, f) == NULL ||
  127.              sscanf(Line, "%d %d %d %d",
  128.                 &LibItemStruct -> Transform[0][0],
  129.                 &LibItemStruct -> Transform[0][1],
  130.                 &LibItemStruct -> Transform[1][0],
  131.                 &LibItemStruct -> Transform[1][1]) != 4)) {
  132.             Failed = TRUE;
  133.             break;
  134.         }
  135.  
  136.         if (!Failed) {
  137.             LibItemStruct -> Pnext = EEDrawList;
  138.             EEDrawList = (DrawGenericStruct *) LibItemStruct;
  139.         }
  140.         break;
  141.         case 'P':                     /* Its a polyline item. */
  142.         PolylineStruct = (DrawPolylineStruct *)
  143.                     MyMalloc(sizeof(DrawPolylineStruct));
  144.         PolylineStruct -> StructType = DRAW_POLYLINE_STRUCT_TYPE;
  145.         if (sscanf(&Line[1], "%s %d %d",
  146.                Name1,
  147.                &PolylineStruct->Layer,
  148.                &PolylineStruct -> NumOfPoints) != 3 ||
  149.             (Name1[0] != 'B' && Name1[0] != 'L')) {
  150.             sprintf(Line,
  151.             "EEDraw file polyline struct error at line %d, aborted",
  152.                                 LineCount);
  153.             Failed = TRUE;
  154.             break;
  155.         }
  156.         PolylineStruct -> Width =
  157.             Name1[0] == 'B' ? THICK_WIDTH : NORM_WIDTH;
  158.         PolylineStruct -> Points = (int *) MyMalloc(sizeof(int) * 2 *
  159.                         PolylineStruct -> NumOfPoints);
  160.         for (i = 0; i < PolylineStruct -> NumOfPoints; i++) {
  161.             LineCount++;
  162.             if (fgets(Line, LINE_LEN - 1, f) == NULL ||
  163.             sscanf(Line, "%d %d", &PolylineStruct -> Points[i*2],
  164.                           &PolylineStruct -> Points[i*2+1])
  165.                                 != 2) {
  166.             sprintf(Line,
  167.              "EEDraw file polyline struct error at line %d, aborted",
  168.                                 LineCount);
  169.             Failed = TRUE;
  170.             MyFree((VoidPtr) PolylineStruct -> Points);
  171.             break;
  172.             }
  173.         }
  174.  
  175.         if (!Failed) {
  176.             PolylineStruct -> Pnext = EEDrawList;
  177.             EEDrawList = (DrawGenericStruct *) PolylineStruct;
  178.         }
  179.         break;
  180.         case 'C':                   /* Its a connection item. */
  181.         ConnectionStruct = (DrawConnectionStruct *)
  182.                     MyMalloc(sizeof(DrawConnectionStruct));
  183.         ConnectionStruct -> StructType = DRAW_CONNECTION_STRUCT_TYPE;
  184.         if (sscanf(&Line[1], "%d %d %d",&ConnectionStruct->Layer,
  185.                &ConnectionStruct -> PosX,
  186.                &ConnectionStruct -> PosY) != 3) {
  187.             sprintf(Line,
  188.               "EEDraw file connection struct error at line %d, aborted",
  189.                                 LineCount);
  190.             Failed = TRUE;
  191.         }
  192.         else {
  193.             ConnectionStruct -> Pnext = EEDrawList;
  194.             EEDrawList = (DrawGenericStruct *) ConnectionStruct;
  195.         }
  196.         break;
  197.         case 'T':                         /* Its a text item. */
  198.         TextStruct = (DrawTextStruct *) MyMalloc(sizeof(DrawTextStruct));
  199.         TextStruct -> StructType = DRAW_TEXT_STRUCT_TYPE;
  200.         TextStruct -> Scale = 1;    /* seed scale */
  201.         if ((((i = sscanf(&Line[1], "%d %d %d %d %d",
  202.                   &TextStruct->Layer,
  203.                        &TextStruct -> PosX,
  204.                   &TextStruct -> PosY,
  205.                   &TextStruct -> Orient,
  206.                   &TextStruct -> Scale)) != 4) && i != 5) ||
  207.             fgets(Line, LINE_LEN - 1, f) == NULL) {
  208.             sprintf(Line,
  209.               "EEDraw file text struct error at line %d, aborted",
  210.                                 LineCount);
  211.             Failed = TRUE;
  212.         }
  213.         else {
  214.             TextStruct -> Text = strdup(strtok(Line, "\n\r"));
  215.             TextStruct -> Pnext = EEDrawList;
  216.             EEDrawList = (DrawGenericStruct *) TextStruct;
  217.         }
  218.         if (i == 3) TextStruct -> Scale = 1;  /* No scale specified. */
  219.         break;
  220.         default:
  221.         Failed = FALSE;
  222.         sprintf(Line, "EEDraw file undef structdef at line %d, aborted",
  223.                                 LineCount);
  224.         break;
  225.     }
  226.  
  227.     if (Failed) {
  228.         FatalError(Line);
  229.     }
  230.     }
  231.  
  232.     /* EEDrawList was constructed in reverse order - reverse it back: */
  233.     Phead = NULL;
  234.     while (EEDrawList) {
  235.     Pnext = EEDrawList;
  236.     EEDrawList = EEDrawList -> Pnext;
  237.     Pnext -> Pnext = Phead;
  238.     Phead = Pnext;
  239.     }
  240.     EEDrawList = Phead;
  241.  
  242.     fclose(f);
  243. }
  244.